import 'package:flutter/material.dart';
import 'package:flutter_shop_z/config/service_url.dart';
import 'package:flutter_shop_z/utils/SwiperDiyUtil.dart';
import '../service/service_method.dart';
import 'dart:convert'; //json解析
import 'package:flutter_shop_z/pages/home_page/TopNavigator.dart';
import 'package:flutter_shop_z/pages/home_page/AdBanner.dart';
import 'package:flutter_shop_z/pages/home_page/ShopInfoImg.dart';
import 'package:flutter_shop_z/pages/home_page/Recommend.dart';
import 'package:flutter_shop_z/pages/home_page/FloorContent.dart';
import 'package:flutter_shop_z/pages/home_page/HotGoods.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

class HomePage extends StatefulWidget {
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive =>
      true; //保持页面数据，不重复加载,还要实现AutomaticKeepAliveClientMixin

  List hotGoodsList;
  int page = 1;

  @override
  void initState() {
    super.initState();
    getCommodityPage(page).then((value) {
      hotGoodsList = (value['data'] as List).cast();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("快乐购购购"),
        backgroundColor: Colors.purple,
      ),
      body: FutureBuilder(
        future: getUrl(servicePath['homePage']),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            // var data = json.decode(snapshot.data.toString());
            List<Map> swiperDataList =
                (snapshot.data['data']['commodityList'] as List).cast();
            List<Map> navDataList =
                (snapshot.data['data']['categoryList'] as List).cast();
            String adImg = snapshot.data['data']['adBean']['adImg'];
            String shopInfoPhone =
                snapshot.data['data']['shopInfo']['shopInfoPhone'];
            String shopInfoImg =
                snapshot.data['data']['shopInfo']['shopInfoImg'];
            List<Map> recommendList =
                (snapshot.data['data']['recommendList'] as List).cast();
//楼层
            List<Map> floorTitleList =
                (snapshot.data['data']['floorCategoryList'] as List).cast();

            List<Map> floorGoodsListOne =
                (snapshot.data['data']['floorCommodityListOne'] as List).cast();
            List<Map> floorGoodsListTow =
                (snapshot.data['data']['floorCommodityListTow'] as List).cast();
            List<Map> floorGoodsListThree =
                (snapshot.data['data']['floorCommodityListThree'] as List)
                    .cast();
            List<Map> hotGoodsMore;

            return EasyRefresh(
              child: ListView(
                children: <Widget>[
                  SiwperDiy(swiperDataList: swiperDataList),
                  TopNavigator(navDataList),
                  AdBanner(adImg),
                  ShopInfoImg(
                      shopInfoImgUrl: shopInfoImg,
                      shopInfoPhone: shopInfoPhone),
                  Recommend(recommendList),
                  FloorContent(floorTitleList, floorGoodsListOne,
                      floorGoodsListTow, floorGoodsListThree),
                  HotGoods(hotGoodsList),
                ],
              ),
              onLoad: () async {
                print('数据加载更多');
                var formData = {"page": ++page};
                await getCommodityPage(++page).then((value) => {
                      hotGoodsMore = (value['data'] as List).cast(),
                      setState(() {
                        hotGoodsList.addAll(hotGoodsMore);
                      })
                    });
              },
              footer: ClassicalFooter(
                loadingText: '数据加载中',
                alignment: Alignment.center,
                loadedText: '商品完毕',
                loadFailedText: "加载失败",
              ),
            );
          } else {
            return Center(
              child: Text("加载中"),
            );
          }
        },
      ),
    );
  }
}
